<?php

namespace App\Controller\admin;

use App\Model\Feedback;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;

/**
 * Class FeedbackController
 * @package App\Controller\admin
 * @Controller(prefix="a/feedback")
 * @Middleware(\App\Middleware\AdminMiddleware::class)
 */
class FeedbackController extends AdminBase {
    /**
     * 反馈列表
     * @return array
     * @RequestMapping(path="lists",methods="get")
     */
    public function getLists() {
        $reply = $this->get('reply');
        $time = $this->get('time');
        $mobile = $this->get('mobile');
        $size = pageSizeVerify($this->get('size'));
        $condition = [['is_delete', '=', 0]];
        switch ($reply) {
            case 'yes':
                $condition[] = ['is_reply', '=', 1];
                break;
            case 'no':
                $condition[] = ['is_reply', '=', 0];
                break;
        }
        $time = explode(',', $time);
        if (count($time) == 2) {
            $time[0] = $time[0] . ' 00:00:00';
            $time[1] = $time[1] . ' 23:59:59';
            $condition[] = ['f.create_time', 'between', $time];
        }
        if ($mobile) {
            $condition[] = ['mobile', '=', $mobile];
        }
        $result = Feedback::query()
            ->from('feedback as f')
            ->join('user as u', 'uid', '=', 'u.id')
            ->orderBy('f.id', 'desc')
            ->where($condition)
            ->paginate($size, ['f.id', 'uid', 'nick_name', 'u.mobile', 'images', 'content', 'is_reply', 'f.create_time']);
        $result = paginate($result);
        return success($result);
    }

    /**
     * 回复反馈
     * @return array
     * @RequestMapping(path="info",methods="post")
     */
    public function postInfo() {
        $content = $this->body('content');
        $id = (int) $this->body('id');
        $len = mb_strlen($content);
        if ($len == 0 || $len > 500) {
            return error('回复内容为1-500个字符');
        }
        $admin = $this->request->getAttribute('admin');
        $res = Feedback::query()
            ->where('id', $id)
            ->where('is_delete', 0)
            ->update([
                'is_reply' => 1,
                'admin_id' => $admin['id'],
                'admin_name' => $admin['name'],
                'reply_content' => $content,
                'reply_time' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']),
            ]);
        if ($res !== false) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 查询反馈详情
     * @return array
     * @RequestMapping(path="info",methods="get")
     */
    public function getInfo() {
        $id = (int) $this->get('id');
        $info = Feedback::query()
            ->where('id', $id)
            ->where('is_delete', 0)
            ->select('admin_name', 'is_reply', 'reply_content', 'reply_time')
            ->first();
        if (empty($info)) {
            return error('反馈记录不存在');
        }
        return success($info);
    }

    /**
     * 删除反馈
     * @return array
     * @RequestMapping(path="info",methods="delete")
     */
    public function deleteInfo() {
        $id = (int) $this->body('id');
        $admin = $this->request->getAttribute('admin');
        $res = Feedback::query()
            ->where('id', $id)
            ->where('is_delete', 0)
            ->update([
                'is_delete' => 1,
                'admin_id' => $admin['id'],
                'admin_name' => $admin['name'],
            ]);
        if ($res) {
            return success();
        } else {
            return error();
        }
    }
}
